<!-- Copyright 2012 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<title>Unit Test of e2e.openpgp.EncryptedCipher</title>
<script src="../../../closure/base.js"></script>
<script src="test_js_deps-runfiles.js"></script>
<script>
  goog.require('goog.testing.jsunit');
  goog.require('e2e.openpgp.EncryptedCipher');
  goog.require('e2e.openpgp.MPI');
  goog.require('e2e.openpgp.S2K');
  goog.require('e2e.cipher.Algorithm');
  goog.require('e2e.cipher.AES');
  goog.require('e2e.cipher.RSA');
  goog.require('e2e.hash.Sha1');
</script>
<script>
  function testEncryptedCipher() {
    var fakeCipher = new e2e.cipher.AES(
        e2e.cipher.Algorithm.AES128,
        {key: goog.array.repeat(0x16, 16)});
    var ec = new e2e.openpgp.EncryptedCipher(
        goog.array.repeat(0xDD, 32),
        e2e.openpgp.EncryptedCipher.KeyDerivationType.S2K_SHA1,
        fakeCipher,
        e2e.cipher.Algorithm.AES128,
        goog.array.repeat(0xFF, 16),
        new e2e.openpgp.SimpleS2K(
            new e2e.hash.Sha1));
    assertNotThrows('Checks that public operations are allowed.', function() {
      ec.encrypt([]);
    });
    assertThrows('Checks that private operations are not allowed.', function() {
      ec.decrypt([]);
    });
  }
  function testLockAndUnlockKey() {
    var serializedKey = [
      0x04, 0x00, 0xa8, 0xb3, 0xb2, 0x84, 0xaf, 0x8e, 0xb5, 0x0b, 0x38, 0x70, 0x34, 0xa8, 0x60,
      0xf1, 0x46, 0xc4, 0x91, 0x9f, 0x31, 0x87, 0x63, 0xcd, 0x6c, 0x55, 0x98, 0xc8, 0xae, 0x48,
      0x11, 0xa1, 0xe0, 0xab, 0xc4, 0xc7, 0xe0, 0xb0, 0x82, 0xd6, 0x93, 0xa5, 0xe7, 0xfc, 0xed,
      0x67, 0x5c, 0xf4, 0x66, 0x85, 0x12, 0x77, 0x2c, 0x0c, 0xbc, 0x64, 0xa7, 0x42, 0xc6, 0xc6,
      0x30, 0xf5, 0x33, 0xc8, 0xcc, 0x72, 0xf6, 0x2a, 0xe8, 0x33, 0xc4, 0x0b, 0xf2, 0x58, 0x42,
      0xe9, 0x84, 0xbb, 0x78, 0xbd, 0xbf, 0x97, 0xc0, 0x10, 0x7d, 0x55, 0xbd, 0xb6, 0x62, 0xf5,
      0xc4, 0xe0, 0xfa, 0xb9, 0x84, 0x5c, 0xb5, 0x14, 0x8e, 0xf7, 0x39, 0x2d, 0xd3, 0xaa, 0xff,
      0x93, 0xae, 0x1e, 0x6b, 0x66, 0x7b, 0xb3, 0xd4, 0x24, 0x76, 0x16, 0xd4, 0xf5, 0xba, 0x10,
      0xd4, 0xcf, 0xd2, 0x26, 0xde, 0x88, 0xd3, 0x9f, 0x16, 0xfb, 0x00, 0x11, 0x01, 0x00, 0x01,
      0x03, 0xff, 0x53, 0x33, 0x9c, 0xfd, 0xb7, 0x9f, 0xc8, 0x46, 0x6a, 0x65, 0x5c, 0x73, 0x16,
      0xac, 0xa8, 0x5c, 0x55, 0xfd, 0x8f, 0x6d, 0xd8, 0x98, 0xfd, 0xaf, 0x11, 0x95, 0x17, 0xef,
      0x4f, 0x52, 0xe8, 0xfd, 0x8e, 0x25, 0x8d, 0xf9, 0x3f, 0xee, 0x18, 0x0f, 0xa0, 0xe4, 0xab,
      0x29, 0x69, 0x3c, 0xd8, 0x3b, 0x15, 0x2a, 0x55, 0x3d, 0x4a, 0xc4, 0xd1, 0x81, 0x2b, 0x8b,
      0x9f, 0xa5, 0xaf, 0x0e, 0x7f, 0x55, 0xfe, 0x73, 0x04, 0xdf, 0x41, 0x57, 0x09, 0x26, 0xf3,
      0x31, 0x1f, 0x15, 0xc4, 0xd6, 0x5a, 0x73, 0x2c, 0x48, 0x31, 0x16, 0xee, 0x3d, 0x3d, 0x2d,
      0x0a, 0xf3, 0x54, 0x9a, 0xd9, 0xbf, 0x7c, 0xbf, 0xb7, 0x8a, 0xd8, 0x84, 0xf8, 0x4d, 0x5b,
      0xeb, 0x04, 0x72, 0x4d, 0xc7, 0x36, 0x9b, 0x31, 0xde, 0xf3, 0x7d, 0x0c, 0xf5, 0x39, 0xe9,
      0xcf, 0xcd, 0xd3, 0xde, 0x65, 0x37, 0x29, 0xea, 0xd5, 0xd1, 0x02, 0x00, 0xd3, 0x27, 0x37,
      0xe7, 0x26, 0x7f, 0xfe, 0x13, 0x41, 0xb2, 0xd5, 0xc0, 0xd1, 0x50, 0xa8, 0x1b, 0x58, 0x6f,
      0xb3, 0x13, 0x2b, 0xed, 0x2f, 0x8d, 0x52, 0x62, 0x86, 0x4a, 0x9c, 0xb9, 0xf3, 0x0a, 0xf3,
      0x8b, 0xe4, 0x48, 0x59, 0x8d, 0x41, 0x3a, 0x17, 0x2e, 0xfb, 0x80, 0x2c, 0x21, 0xac, 0xf1,
      0xc1, 0x1c, 0x52, 0x0c, 0x2f, 0x26, 0xa4, 0x71, 0xdc, 0xad, 0x21, 0x2e, 0xac, 0x7c, 0xa3,
      0x9d, 0x02, 0x00, 0xcc, 0x88, 0x53, 0xd1, 0xd5, 0x4d, 0xa6, 0x30, 0xfa, 0xc0, 0x04, 0xf4,
      0x71, 0xf2, 0x81, 0xc7, 0xb8, 0x98, 0x2d, 0x82, 0x24, 0xa4, 0x90, 0xed, 0xbe, 0xb3, 0x3d,
      0x3e, 0x3d, 0x5c, 0xc9, 0x3c, 0x47, 0x65, 0x70, 0x3d, 0x1d, 0xd7, 0x91, 0x64, 0x2f, 0x1f,
      0x11, 0x6a, 0x0d, 0xd8, 0x52, 0xbe, 0x24, 0x19, 0xb2, 0xaf, 0x72, 0xbf, 0xe9, 0xa0, 0x30,
      0xe8, 0x60, 0xb0, 0x28, 0x8b, 0x5d, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x3E];
    var fakeCipher = new e2e.cipher.RSA(
      e2e.cipher.Algorithm.RSA,
      {
        'n': goog.array.clone(e2e.openpgp.MPI.parse(serializedKey)),
        'e': goog.array.clone(e2e.openpgp.MPI.parse(serializedKey))
      });
    var ec = new e2e.openpgp.EncryptedCipher(
        serializedKey,
        e2e.openpgp.EncryptedCipher.KeyDerivationType.PLAINTEXT,
        fakeCipher);
    var plaintext = goog.array.repeat(0xBB, 40);
    var passphrase = [1, 2, 3, 4];
    ec.unlockKey();
    var ciphertext = e2e.async.Result.getValue(ec.encrypt(plaintext));
    assertArrayEquals(plaintext, e2e.async.Result.getValue(ec.decrypt(ciphertext)));
    ec.lockKey(passphrase);
    assertArrayEquals(plaintext, e2e.async.Result.getValue(ec.decrypt(ciphertext)));
    var fakeCipher2 = new e2e.cipher.RSA(e2e.cipher.Algorithm.RSA, {
      e: fakeCipher.getKey().e,
      n: fakeCipher.getKey().n
    });
    var ec2 = new e2e.openpgp.EncryptedCipher(
        ec.encryptedKeyData,
        ec.getKeyDerivationType(),
        fakeCipher2,
        ec.getKeyDerivationAlgorithm(),
        ec.getKeyDerivationIv(),
        ec.getKeyDerivationS2k());
    assertThrows(function() {
      ec2.unlockKey();
    });
    assertThrows(function() {
      ec2.decrypt(ciphertext);
    });
    assertThrows(function() {
      ec2.unlockKey(passphrase.concat([0]));
    });
    assertThrows(function() {
      var wrongPassphrase = passphrase.slice();
      wrongPassphrase[0] ^= 0xF1;
      ec2.unlockKey(wrongPassphrase);
    });
    ec2.unlockKey(passphrase);
    assertArrayEquals(plaintext, e2e.async.Result.getValue(ec2.decrypt(ciphertext)));
  }
</script>
